"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库系统
Case Name   : 删除pid文件权限，进行数据恢复
Create At   : 2020/07/24
@zou_jialiang0505328126
Description :
    1.修改pid文件权限
    2.导出数据
    3.数据恢复
    4.恢复pid文件权限
    5.导出数据
    6.数据恢复
Expect      :
    1.修改pid文件权限000成功
    2.导出数据成功
    3.数据恢复成功
    4.恢复pid文件权限成功
    5.导出数据成功
    6.数据恢复成功
History     :
    modified：2021/02/02  modified by @peilinqian，修改整个脚本逻辑
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DbsysCase130(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info("----this is setup----")
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_1_sh = CommonSH('Standby1DbUser')
        self.sta_2_sh = CommonSH('Standby2DbUser')
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.pri_root = Node(node='PrimaryRoot')
        self.sta1_dbuser = Node(node='Standby1DbUser')
        self.sta1_root = Node(node='Standby1Root')
        self.com = Common()
        self.constant = Constant()
        self.pid_file = os.path.join(macro.DB_INSTANCE_PATH,
                                     macro.DB_POSTMASTER_PID_NAME)

        dump_file_name = 'test_pid.tar'
        self.file_name = os.path.join(macro.DB_INSTANCE_PATH, dump_file_name)

        self.log.info("-----创建restore数据库----")
        self.new_db_name = 'restore_db'
        self.create_db_cmd = f'drop database if exists {self.new_db_name};' \
            f'create database {self.new_db_name};'
        self.drop_db_cmd = f'drop database {self.new_db_name};'
        create_db_result = self.pri_sh.executDbSql(self.create_db_cmd)
        self.log.info(create_db_result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS_MSG,
                      create_db_result)

        self.log.info('----Opengauss_Reliability_Dbsys_Case130 start----')

    def test_main(self):
        self.log.info("----1.删除pid文件权限 ----")
        chmod_cmd = f'chmod 000 {self.pid_file};ls -al {self.pid_file}'
        chmod_result = self.pri_dbuser.sh(chmod_cmd).result()
        self.log.info(chmod_result)

        self.log.info('----2.dump file----')
        dump_result = self.pri_sh.dumpFile(self.file_name, get_detail=False)
        self.log.info(dump_result)
        self.assertTrue(dump_result)

        self.log.info('----3.进行restore----')
        restore_result = self.pri_sh.restoreFile(self.file_name,
                                                 '',
                                                 True,
                                                 self.new_db_name)
        self.assertNotIn(self.constant.RESTORE_FAIL_MSG, restore_result)

        self.log.info("----4.恢复pid文件权限 ----")
        chmod_cmd = f'chmod 600 {self.pid_file};ls -al {self.pid_file}'
        chmod_result = self.pri_dbuser.sh(chmod_cmd).result()
        self.log.info(chmod_result)

        self.log.info('----5.dump file----')
        dump_result = self.pri_sh.dumpFile(self.file_name, get_detail=False)
        self.log.info(dump_result)
        self.assertTrue(dump_result)

        self.log.info('----6.进行restore----')
        restore_result = self.pri_sh.restoreFile(self.file_name,
                                                 '-c',
                                                 True,
                                                 self.new_db_name)
        self.assertNotIn(self.constant.RESTORE_FAIL_MSG, restore_result)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        self.log.info("----恢复pid文件权限 ----")
        chmod_cmd = f'chmod 600 {self.pid_file};ls -al {self.pid_file}'
        chmod_result = self.pri_dbuser.sh(chmod_cmd).result()
        self.log.info(chmod_result)

        self.log.info('----delete dump file----')
        delete_cmd = f'rm -rf {self.file_name};ls -al {self.file_name}'

        delete_result = self.pri_dbuser.sh(delete_cmd).result()
        self.log.info(delete_result)

        self.log.info('----Opengauss_Reliability_Dbsys_Case130 执行结束----')
